package com.firefly.mq.redisMq.container;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.firefly.dao.util.RedisUtils;
import com.firefly.mq.redisMq.job.DelayJob;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

/**
 * @description: 待完成任务，内部使用topic进行细分，每个topic对应一个list集合
 * @author: lzg(萤火科技 ： 854378082 @ qq.com)
 * @date: 2022/10/28
 **/
@Component
@Slf4j
public class ReadyQueue {

    @Autowired
    private RedisUtils redisUtils;

    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 设置任务
     * @param delayJob
     */
    public void pushJob(DelayJob delayJob) {
        log.info("执行队列添加任务:{}",delayJob);
        redisUtils.lSet("process-queue:" + delayJob.getTopic(),delayJob);
        // 告诉订阅者去执行
        redisTemplate.convertAndSend("redisSub",delayJob.getTopic());
    }

    /**
     * 获得任务
     * @param topic
     * @return
     */
    public DelayJob getJob(String topic) throws JsonProcessingException {
        Object o = redisUtils.lGetIndex("process-queue:" + topic,0);
        if (o instanceof DelayJob) {
//            log.info("执行队列取出任务:{}", JsonUtil.obj2Str((DelayJob) o));
            return (DelayJob) o;
        }
        return null;
    }
    /*
    * 移除任务
    * */
    public void popJob(String topic) throws JsonProcessingException {
        redisUtils.lPop("process-queue:" + topic);
    }
}


